package com.erp.erpboot.controller.PU;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.erp.erpboot.factory.ApplicationContextFactory;
import com.erp.erpboot.mapper.PuOrderMapper;
import com.erp.erpboot.pojo.PuOrder;
import com.erp.erpboot.pojo.DataObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/puorder")
public class PuOrderController {
    @Autowired
    PuOrderMapper puOrderMapper;

    ApplicationContext context = ApplicationContextFactory.getApplicationContext();

    @RequestMapping({"/", "/index"})
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView("puorder/voucher");
        List<PuOrder> puOrder = puOrderMapper.list();

        mav.addObject("data", JSON.toJSONString(puOrder, SerializerFeature.WriteMapNullValue));
        return mav;
    }

    @RequestMapping(value = {"/get"}, method = RequestMethod.GET)
    @ResponseBody
    public String get(int id) throws Exception {
//        ModelAndView mav = new ModelAndView("puorder/voucher");
        PuOrder puOrder = puOrderMapper.get(id);
//        mav.addObject("data", JSON.toJSONString(puOrder));
        if (puOrder==null){
            puOrder=new PuOrder();
        }
        String str=JSON.toJSONString(puOrder, SerializerFeature.WriteMapNullValue);
        return str;
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView create(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView("puorder/create");
        return mav;
    }

    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public DataObject create(PuOrder puOrder) throws Exception {
        puOrderMapper.add(puOrder);

        DataObject dataObject = (DataObject) context.getBean("dataObject");

        return dataObject;
    }

    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public DataObject update(PuOrder puOrder) throws Exception {
        puOrderMapper.update(puOrder);

        DataObject dataObject = (DataObject) context.getBean("dataObject");

        return dataObject;
    }

    public ModelAndView addNewVoucher(PuOrder newData) throws Exception {
        ModelAndView mav=new ModelAndView("puorder/voucher");
        PuOrder puOrder=newData;
        mav.addObject("data", JSON.toJSONString(puOrder));
        return mav;
    }
}
